package 题目集.图.遍历模拟;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

//https://pintia.cn/problem-sets/1745607712521764864/exam/problems/1745607748311744523?type=7&page=0
public class 哲哲打游戏 {
    static final int[] record = new int[101];  //  档案
    static final int[][] map = new int[100001][]; //邻接表法存储图。（并不是临界矩阵）

    public static void main(String[] args) throws IOException {
        Reader sc = new Reader();
        int n = sc.nextInt();
        int m = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            int k = sc.nextInt();
            map[i] = new int[k + 1];
            for (int j = 1; j < map[i].length; j++) {
                map[i][j] = sc.nextInt();
            }
        }
        int cur = 1;
        while (m-- > 0) {
            int type = sc.nextInt();    //  操作类型
            int op = sc.nextInt();  //  操作
            switch (type) {
                case 0:
                    cur = map[cur][op];
                    break;
                case 1:
                    record[op] = cur;   //  存档
                    System.out.println(cur);
                    break;
                case 2:
                    cur = record[op];   //  读档
                    break;
            }
        }
        System.out.println(cur);
    }

    static class Reader {

        static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        static int nextInt() throws IOException {
            in.nextToken();
            return (int) in.nval;
        }
    }

}

